package com.test.work.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.work.dto.AjaxMsgDto;
import com.test.work.service.IUserService;

@Controller
@RequestMapping("/t_user")
public class UserController {
	private static Logger logger = Logger.getLogger(UserController.class);

	@Resource
	private IUserService userService;

	@RequestMapping("/login")
	@ResponseBody
	public AjaxMsgDto login(String username, String password) {
		AjaxMsgDto ajaxMsgDto = new AjaxMsgDto();
		try {
			ajaxMsgDto.setIsSuccess(true);
			Object info = userService.login(username,password);
			if(info != null){				
				ajaxMsgDto.setData(info);
			}
			else;
			logger.info(ajaxMsgDto.toString());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxMsgDto.setIsSuccess(false);
		}
		return ajaxMsgDto;
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public AjaxMsgDto register(String username, String password) {
		AjaxMsgDto ajaxMsgDto = new AjaxMsgDto();
		try {
			ajaxMsgDto.setIsSuccess(true);
			if(username.equals("") || password.equals(""))
			{
				ajaxMsgDto.setData("账号密码为空");
			}
			else
			{
				Object info = userService.register(username,password);
				if(info != null){				
					ajaxMsgDto.setData(info);
				}
				else;
			}
			logger.info(ajaxMsgDto.toString());
		} catch (Exception e) {
			e.printStackTrace();
			ajaxMsgDto.setIsSuccess(false);
		}
		return ajaxMsgDto;
	}
	
}
